<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');
App::uses('Content', 'Model');
App::uses('I18n', 'I18n');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

	/**
	 * Components
	 *
	 * @var array
	 */
	public $components = array(
		'Session',
		//'Security' => array(
		//	'blackHoleCallback' => 'blackHole'
	//	),
		'Dashboard',
		'Auth'
	);

	/**
	 * Helpers
	 *
	 * @var array
	 */
	public $helpers = array(
		'Session',
		'Html',
		'Form'
	);

	public function beforeRender() {
		if ($this->name == 'CakeError') {
			Configure::write('App.error', true);
		}
	}

	public function beforeFilter() {
		Configure::write('Config.language', 'fr');
		parent::beforeFilter();
		Configure::load('config');

		$this->Auth->authorize = 'Controller';
		$this->Auth->loginAction = array(
			'controller' => 'users',
			'action' => 'login',
			'admin' => false
		);
		$this->Auth->flash = array(
			'element' => 'flash',
			'key' => 'flash',
			'params' => array(
				'type' => 'error',
				'title' => __('Security Error')
			)
		);
		$this->Auth->authenticate = array(
			'all' => array(
				'userModel' => 'User',
				'scope' => array(
					'User.active' => true,
					'User.email_verified' => true
				)
			),
			'Form'
		);
		if (preg_match('/(index|report)$/', $this->request['action'])) {
			$this->Components->disable('Security');
		}
		if (empty($this->request->params['admin'])) {
			$this->Auth->allow();
		}
	}

	public function isAuthorized($user = null) {
		// Only admins can access admin functions
		if (isset($this->request->params['admin'])) {
			return (bool) ($user['role_id'] == 1);
		}

		// Default deny
		return false;
	}

	/**
	 * Redirects to given $url, after turning off $this->autoRender.
	 * Script execution is halted after the redirect.
	 *
	 * @param mixed $url A string or array-based URL pointing to another location within the app,
	 *     or an absolute URL
	 * @param integer $status Optional HTTP status code (eg: 404)
	 * @param boolean $exit If true, exit() will be called after the redirect
	 * @return mixed void if $exit = false. Terminates script if $exit = true
	 * @todo remove it if cakephp fix http://cakephp.lighthouseapp.com/projects/42648/tickets/2917-cakeresponse-problem-in-develop-mode
	 */
	public function redirect($url, $status = null, $exit = true) {
		if (headers_sent()) {
			header('header-already-sent', true);
		}
		parent::redirect($url, $status, $exit);
	}

	/**
	 * Used to set a session variable that can be used to output messages in the view.
	 *
	 * @param string $message Message to be flashed
	 * @param array $params Parameters to be sent to layout as view variables
	 * @param string $key Message key, default is 'flash'
	 * @param string $element Element to wrap flash message in.
	 * @return void
	 */
	public function setFlash($message, $type = 'error', $key = 'flash') {
		$maps = array(
			'error' => __('Error'),
			'info' => __('Information'),
			'warning' => __('Warning'),
			'success' => __('Success')
		);
		$params = array(
			'type' => $type,
			'title' => $maps[$type]
		);
		$this->Session->setFlash($message, 'flash', $params, $key);
	}

	/**
	 * Security callback hole.
	 *
	 * @param string $error
	 * @return void
	 */
	public function blackHole($error = null) {
		//debug($error);exit;
		switch (strtolower($error)) {
			case 'auth' : {
					$this->setFlash(__('Security error , data sent to the server by the browser is not valid authentication. Please try again'));
					$this->redirect($this->request->here(false), null, true);
				}
			case 'csrf' : {
					$this->setFlash(__('Security error , Time pending submission of your form has expired or is your data has been sent to the server is duplicated . Please try again'));
					$this->redirect($this->request->here(false), null, true);
				}
			default : {
					throw new SecurityException(
					__('Security error , The actions that require a request that is %s.', array(Inflector::humanize($error)))
					);
				}
		}
	}

	public function setPageTitle($title) {
		$this->set('title_for_layout', $title);
	}

	/**
	 * Debug
	 * 
	 * @return void
	 */
	public function showDebug() {
		include CAKE . DS . 'View' . DS . 'Elements' . DS . 'sql_dump.ctp';
		exit();
	}

}
